/*
 * Copyright 2007 Rob Nielsen
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.proxymatic.apps.autoboundary;

import com.googlecode.proxymatic.apps.autoboundary.handlers.AutoboundaryConstructorMethodHandler;
import com.googlecode.proxymatic.apps.autoboundary.handlers.AutoboundaryMethodHandler;
import com.googlecode.proxymatic.apps.autoboundary.handlers.AutoboundaryReverseMethodHandler;
import com.googlecode.proxymatic.core.AbstractProxyApplication;
import com.googlecode.proxymatic.core.HandlerConfiguration;
import com.googlecode.proxymatic.core.handlers.exception.WrappedExceptionHandler;
import com.googlecode.proxymatic.core.handlers.method.ImplementationObjectMethodHandler;

public class ProxyAutoboundaryHelper extends AbstractProxyApplication implements AutoboundaryHelper {
    private AutoboundaryLookup autoboundaryLookup;

    public ProxyAutoboundaryHelper(AutoboundaryLookup autoboundaryLookup) {
        this.autoboundaryLookup = autoboundaryLookup;
    }

    public ProxyAutoboundaryHelper() {
        this(new AutoboundaryLookup());
    }

    protected void setUpHandlerConfiguration(HandlerConfiguration handlerConfiguration) {
        handlerConfiguration.addHandler(new AutoboundaryMethodHandler(autoboundaryLookup));
        handlerConfiguration.addHandler(new AutoboundaryReverseMethodHandler(autoboundaryLookup));
        handlerConfiguration.addHandler(new AutoboundaryConstructorMethodHandler(autoboundaryLookup));
        handlerConfiguration.addHandler(new ImplementationObjectMethodHandler());
        handlerConfiguration.addHandler(new WrappedExceptionHandler());
    }

    protected String getProxyType() {
        return "Autoboundary";
    }

    public <T> T create(Class<T> autoboundaryClass) {
        Class realClass = autoboundaryLookup.getRealClass(autoboundaryClass);
        return autoboundaryClass.cast(create(proxy()
                .implementingInterface(autoboundaryClass)
                .withImplementationClass(realClass)));
    }

    public <T> T wrap(Class<T> autoboundaryClass, Object realObject) {
        if (autoboundaryLookup.isAutoboundaryFactoryClass(autoboundaryClass)) {
            throw new IllegalArgumentException(autoboundaryClass + " is a factory.  Use create() instead.");
        }
        Class realClass = autoboundaryLookup.getRealClassForInstance(autoboundaryClass);
        if (!realClass.isAssignableFrom(realObject.getClass())) {
            throw new IllegalArgumentException(realObject + " is not an instance of " + realClass + " so can't be transformed into autoboundary " + autoboundaryClass);
        }
        return autoboundaryClass.cast(create(proxy()
                .implementingInterface(autoboundaryClass)
                .withImplementationClass(realClass)
                .withImplementationObject(realObject)));
    }

    public Class getRealClass(Class autoboundaryClass) {
        return autoboundaryLookup.getRealClass(autoboundaryClass);
    }
}
